/*
 * $Id: ASCIOHandler.java,v 1.4 2009/10/15 15:31:14 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;

/**
 * @author 212042764
 * 
 */
public class ASCIOHandler {
	/**
	 * All methods should be static
	 */
	protected ASCIOHandler() {
		super();
	}

	/**
	 * Read all data currently in input stream
	 * 
	 * @param inputStream
	 * @return
	 * @throws IOException
	 */
	public static String readAllCurrentInput(InputStream inputStream)
			throws IOException {
		StringBuffer reply = new StringBuffer();
		byte[] rBuf = new byte[1];
		int rStat = -1;
		while ((inputStream.available()) > 0) {
			rStat = inputStream.read(rBuf);
			if (rStat == -1) {
				return null;
			}
			reply.append((char) rBuf[0]);
		}
		return reply.toString();
	}

	/**
	 * Reads single line from input stream
	 * 
	 * @param inputStream
	 * @return
	 * @throws IOException
	 */
	public static String readSingleLine(InputStream inputStream)
			throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				inputStream));
		String line = null;
		if (!ASCUtil.isNullOrEmptyObject(reader)) {
			line = reader.readLine();
		}
		return line;
	}

	/**
	 * Reads single line from input stream
	 * 
	 * @param reader
	 * @return
	 * @throws IOException
	 */
	public static String readSingleLine(BufferedReader reader)
			throws IOException {
		String line = null;
		if (!ASCUtil.isNullOrEmptyObject(reader)) {
			line = reader.readLine();
		}
		return line;
	}

	/**
	 * This method will read all input and will cause connection to be reset
	 * 
	 * @return
	 * @throws IOException
	 */
	public static List<String> readAllAsList(InputStream inputStream)
			throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				inputStream));
		List<String> response = new ArrayList<String>();
		if (!ASCUtil.isNullOrEmptyObject(reader)) {
			String line = null;
			while ((line = reader.readLine()) != null) {
				response.add(line);
			}
		}
		return response;
	}

	/**
	 * @return Object array which will basically have String contained in them
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static Object[] readStdIn() throws FileNotFoundException,
			IOException {
		InputStreamReader objStdinReader = null;
		BufferedReader objBufferedReader = null;
		Vector<String> objFileContent = new Vector<String>();

		try {
			String strLineContent = null;
			objStdinReader = new InputStreamReader(System.in);
			objBufferedReader = new BufferedReader(objStdinReader);

			if (!objBufferedReader.ready())
				throw new FileNotFoundException("No Stdin available");

			while ((strLineContent = objBufferedReader.readLine()) != null) {
				// Read the content and add to a Vector
				objFileContent.add(strLineContent);
			}
		} catch (FileNotFoundException objFileNotFound) {
			throw objFileNotFound;
		} catch (IOException objIOException) {
			throw objIOException;
		} finally {
			closeReader(objBufferedReader);
			closeReader(objStdinReader);
		}

		return objFileContent.toArray();
	}

	/**
	 * @param objWriter
	 *            Closes the Writer object
	 */
	public static void closeInputStream(InputStream inputStream) {
		String methodName = "closeInputStream";
		try {
			if (!ASCUtil.isNullOrEmptyObject(inputStream)) {
				inputStream.close();
			}
		} catch (IOException objIOException) {
			// Just Log here nothing fancy to be done
			ASCLogger.info(ASCFileHandler.class, methodName,
					"Couldn't close the input stream:" + objIOException.getMessage(), objIOException);
		}

	}

	/**
	 * Closes the output stream if it is not null
	 * 
	 * @param inputStream
	 */
	public static void closeOutputStream(OutputStream outputStream) {
		String methodName = "closeOutputStream";
		try {
			if (!ASCUtil.isNullOrEmptyObject(outputStream)) {
				outputStream.close();
			}
		} catch (IOException objIOException) {
			// Just Log here nothing fancy to be done, info level as we don't want fault tracker for this
			ASCLogger.info(ASCFileHandler.class, methodName,
					"Couldn't close the output stream:" + objIOException.getMessage(), objIOException);
		}

	}

	/**
	 * @param objReader
	 *            Closes open references
	 */
	public static void closeReader(Reader objReader) {
		String methodName = "closeReader";
		try {
			if (!ASCUtil.isNullOrEmptyObject(objReader)) {
				objReader.close();
			}
		} catch (IOException objIOException) {
			// Just Log here nothing fancy to be done
			ASCLogger.info(ASCFileHandler.class, methodName,
					"Couldn't close the input file after reading:" + objIOException.getMessage(), objIOException);
		}

	}

	/**
	 * @param objWriter
	 *            Closes the Writer object
	 */
	public static void closeWriter(Writer objWriter) {
		String methodName = "closeWriter";
		try {
			if (!ASCUtil.isNullOrEmptyObject(objWriter)) {
				objWriter.close();
			}
		} catch (IOException objIOException) {
			// Just Log here nothing fancy to be done
			ASCLogger.info(ASCFileHandler.class, methodName,
					"Couldn't close the error file after writing:" + objIOException.getMessage(), objIOException);
		}

	}
}
